<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>dom_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
  </script>

  <script src="static/js/doc.js">
  </script>

  <meta charset="utf8">
</head>

<body onload="prettyPrint()">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_dom_dom_test.js.html">dom_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line2"></a>// you may not use this file except in compliance with the License.
<a name="line3"></a>// You may obtain a copy of the License at
<a name="line4"></a>//
<a name="line5"></a>//     http://www.apache.org/licenses/LICENSE-2.0
<a name="line6"></a>//
<a name="line7"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line8"></a>// distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
<a name="line9"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line10"></a>// See the License for the specific language governing permissions and
<a name="line11"></a>// limitations under the License.
<a name="line12"></a>
<a name="line13"></a>// Copyright 2009 Google Inc. All Rights Reserved.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Shared code for dom_test.html and dom_quirks_test.html.
<a name="line17"></a> */
<a name="line18"></a>
<a name="line19"></a>goog.require(&#39;goog.dom&#39;);
<a name="line20"></a>goog.require(&#39;goog.dom.DomHelper&#39;);
<a name="line21"></a>goog.require(&#39;goog.dom.NodeType&#39;);
<a name="line22"></a>goog.require(&#39;goog.dom.TagName&#39;);
<a name="line23"></a>goog.require(&#39;goog.testing.asserts&#39;);
<a name="line24"></a>goog.require(&#39;goog.userAgent&#39;);
<a name="line25"></a>
<a name="line26"></a>var $ = goog.dom.$;
<a name="line27"></a>
<a name="line28"></a>// Setup for the iframe
<a name="line29"></a>var myIframe = $(&#39;myIframe&#39;);
<a name="line30"></a>var myIframeDoc = goog.dom.getFrameContentDocument(
<a name="line31"></a>    /** @type {HTMLIFrameElement} */ (myIframe));
<a name="line32"></a>
<a name="line33"></a>// Set up document for iframe: total height of elements in document is 65
<a name="line34"></a>// If the elements are not create like below, IE will get a wrong height for
<a name="line35"></a>// the document.
<a name="line36"></a>myIframeDoc.open();
<a name="line37"></a>// Make sure we progate the compat mode
<a name="line38"></a>myIframeDoc.write((goog.dom.isCss1CompatMode() ? &#39;&lt;!DOCTYPE html&gt;&#39; : &#39;&#39;) +
<a name="line39"></a>    &#39;&lt;style&gt;body{margin:0;padding:0}&lt;/style&gt;&#39; +
<a name="line40"></a>    &#39;&lt;div style=&quot;height:42px;font-size:1px;line-height:0;&quot;&gt;hello world&lt;/div&gt;&#39; +
<a name="line41"></a>    &#39;&lt;div style=&quot;height:23px;font-size:1px;line-height:0;&quot;&gt;hello world&lt;/div&gt;&#39;);
<a name="line42"></a>myIframeDoc.close();
<a name="line43"></a>
<a name="line44"></a>function testDom() {
<a name="line45"></a>  assert(&#39;Dom library exists&#39;, typeof goog.dom != &#39;undefined&#39;);
<a name="line46"></a>}
<a name="line47"></a>
<a name="line48"></a>function testGetElement() {
<a name="line49"></a>  var el = $(&#39;testEl&#39;);
<a name="line50"></a>  assertEquals(&#39;Should be able to get id&#39;, el.id, &#39;testEl&#39;);
<a name="line51"></a>
<a name="line52"></a>  assertEquals($, goog.dom.getElement);
<a name="line53"></a>  assertEquals(goog.dom.$, goog.dom.getElement);
<a name="line54"></a>}
<a name="line55"></a>
<a name="line56"></a>function testGetElementsByTagNameAndClass() {
<a name="line57"></a>  assertEquals(&#39;Should get 6 spans&#39;, goog.dom.$$(&#39;span&#39;).length, 6);
<a name="line58"></a>  assertEquals(&#39;Should get 3 spans&#39;, goog.dom.$$(&#39;span&#39;, &#39;test1&#39;).length, 3);
<a name="line59"></a>  assertEquals(&#39;Should get 1 span&#39;, goog.dom.$$(&#39;span&#39;, &#39;test2&#39;).length, 1);
<a name="line60"></a>  assertEquals(&#39;Should get lots of elements&#39;, goog.dom.$$().length,
<a name="line61"></a>      document.getElementsByTagName(&#39;*&#39;).length);
<a name="line62"></a>
<a name="line63"></a>  assertEquals(&#39;Should get 1 span&#39;, goog.dom.$$(&#39;span&#39;, null,
<a name="line64"></a>                                                $(&#39;testEl&#39;)).length, 1);
<a name="line65"></a>
<a name="line66"></a>  // &#39;*&#39; as the tag name should be equivalent to all tags
<a name="line67"></a>  var container = goog.dom.$(&#39;span-container&#39;);
<a name="line68"></a>  assertEquals(5, goog.dom.$$(&#39;*&#39;, undefined, container).length);
<a name="line69"></a>  assertEquals(3, goog.dom.$$(&#39;*&#39;, &#39;test1&#39;, container).length);
<a name="line70"></a>  assertEquals(1, goog.dom.$$(&#39;*&#39;, &#39;test2&#39;, container).length);
<a name="line71"></a>
<a name="line72"></a>  // Some version of WebKit have problems with mixed-case class names
<a name="line73"></a>  assertEquals(1, goog.dom.$$(undefined, &#39;mixedCaseClass&#39;).length);
<a name="line74"></a>
<a name="line75"></a>  // Make sure that out of bounds indices are OK
<a name="line76"></a>  assertUndefined(goog.dom.$$(undefined, &#39;noSuchClass&#39;)[0]);
<a name="line77"></a>
<a name="line78"></a>  assertEquals(goog.dom.$$, goog.dom.getElementsByTagNameAndClass);
<a name="line79"></a>}
<a name="line80"></a>
<a name="line81"></a>function testSetProperties() {
<a name="line82"></a>  var attrs = { &#39;name&#39;: &#39;test3&#39;, &#39;title&#39;: &#39;A title&#39;, &#39;random&#39;: &#39;woop&#39; };
<a name="line83"></a>  var el = $(&#39;testEl&#39;);
<a name="line84"></a>
<a name="line85"></a>  var res = goog.dom.setProperties(el, attrs);
<a name="line86"></a>  assertEquals(&#39;Should be equal&#39;, el.name, &#39;test3&#39;);
<a name="line87"></a>  assertEquals(&#39;Should be equal&#39;, el.title, &#39;A title&#39;);
<a name="line88"></a>  assertEquals(&#39;Should be equal&#39;, el.random, &#39;woop&#39;);
<a name="line89"></a>}
<a name="line90"></a>
<a name="line91"></a>function testSetPropertiesDirectAttributeMap() {
<a name="line92"></a>  var attrs = {&#39;usemap&#39;: &#39;#myMap&#39;};
<a name="line93"></a>  var el = goog.dom.createDom(&#39;img&#39;);
<a name="line94"></a>
<a name="line95"></a>  var res = goog.dom.setProperties(el, attrs);
<a name="line96"></a>  assertEquals(&#39;Should be equal&#39;, &#39;#myMap&#39;, el.getAttribute(&#39;usemap&#39;));
<a name="line97"></a>}
<a name="line98"></a>
<a name="line99"></a>function testSetTableProperties() {
<a name="line100"></a>  var attrs = { &#39;style&#39;: &#39;padding-left: 10px;&#39;, &#39;class&#39;: &#39;mytestclass&#39;,
<a name="line101"></a>                &#39;height&#39;: &#39;101&#39;, &#39;cellpadding&#39;: &#39;15&#39; };
<a name="line102"></a>  var el = $(&#39;testTable1&#39;);
<a name="line103"></a>
<a name="line104"></a>  var res = goog.dom.setProperties(el, attrs);
<a name="line105"></a>  assertEquals(&#39;Should be equal&#39;, el.style.paddingLeft, &#39;10px&#39;);
<a name="line106"></a>  assertEquals(&#39;Should be equal&#39;, el.className, &#39;mytestclass&#39;);
<a name="line107"></a>  assertEquals(&#39;Should be equal&#39;, el.getAttribute(&#39;height&#39;), &#39;101&#39;);
<a name="line108"></a>  assertEquals(&#39;Should be equal&#39;, el.cellPadding, &#39;15&#39;);
<a name="line109"></a>}
<a name="line110"></a>
<a name="line111"></a>function testGetViewportSize() {
<a name="line112"></a>  // TODO: This is failing in the test runner now, fix later.
<a name="line113"></a>  //var dims = getViewportSize();
<a name="line114"></a>  //assertNotUndefined(&#39;Should be defined at least&#39;, dims.width);
<a name="line115"></a>  //assertNotUndefined(&#39;Should be defined at least&#39;, dims.height);
<a name="line116"></a>}
<a name="line117"></a>
<a name="line118"></a>function testGetViewportSizeInIframe() {
<a name="line119"></a>  var iframe = /** @type {HTMLIFrameElement} */ (goog.dom.$(&#39;iframe&#39;));
<a name="line120"></a>  var contentDoc = goog.dom.getFrameContentDocument(iframe);
<a name="line121"></a>  contentDoc.write(&#39;&lt;body&gt;&lt;/body&gt;&#39;);
<a name="line122"></a>
<a name="line123"></a>  var outerSize = goog.dom.getViewportSize();
<a name="line124"></a>  var innerSize = (new goog.dom.DomHelper(contentDoc)).getViewportSize();
<a name="line125"></a>  assert(&#39;Viewport sizes must not match&#39;,
<a name="line126"></a>      innerSize.width != outerSize.width);
<a name="line127"></a>}
<a name="line128"></a>
<a name="line129"></a>function testGetDocumentHeightInIframe() {
<a name="line130"></a>  var doc = goog.dom.getDomHelper(myIframeDoc).getDocument();
<a name="line131"></a>  var height = goog.dom.getDomHelper(myIframeDoc).getDocumentHeight();
<a name="line132"></a>
<a name="line133"></a>  // Broken in webkit quirks mode.
<a name="line134"></a>  if (goog.dom.isCss1CompatMode_(doc) || !goog.userAgent.WEBKIT) {
<a name="line135"></a>    assertEquals(&#39;height should be 65&#39;, 42 + 23, height);
<a name="line136"></a>  }
<a name="line137"></a>}
<a name="line138"></a>
<a name="line139"></a>function testCreateDom() {
<a name="line140"></a>  var el = goog.dom.$dom(&#39;div&#39;, {
<a name="line141"></a>        style: &#39;border: 1px solid black; width: 50%; background-color: #EEE;&#39;,
<a name="line142"></a>        onclick: &quot;alert(&#39;woo&#39;)&quot;
<a name="line143"></a>      },
<a name="line144"></a>      goog.dom.$dom(&#39;p&#39;, {style: &#39;font: normal 12px arial; color: red; &#39;},
<a name="line145"></a>                    &#39;Para 1&#39;),
<a name="line146"></a>      goog.dom.$dom(&#39;p&#39;, {style: &#39;font: bold 18px garamond; color: blue; &#39;},
<a name="line147"></a>                    &#39;Para 2&#39;),
<a name="line148"></a>      goog.dom.$dom(&#39;p&#39;, {style: &#39;font: normal 24px monospace; color: green&#39;},
<a name="line149"></a>                    &#39;Para 3 &#39;,
<a name="line150"></a>                    goog.dom.$dom(&#39;a&#39;, {
<a name="line151"></a>                          name: &#39;link&#39;, href: &#39;http://bbc.co.uk&#39;
<a name="line152"></a>                      },
<a name="line153"></a>                      &#39;has a link&#39;),
<a name="line154"></a>                    &#39;, how cool is this?&#39;));
<a name="line155"></a>
<a name="line156"></a>  assertEquals(&#39;Tagname should be a DIV&#39;, &#39;DIV&#39;, el.tagName);
<a name="line157"></a>  assertEquals(&#39;Style width should be 50%&#39;, &#39;50%&#39;, el.style.width);
<a name="line158"></a>  assertEquals(&#39;first child is a P tag&#39;, &#39;P&#39;, el.childNodes[0].tagName);
<a name="line159"></a>  assertEquals(&#39;second child .innerHTML&#39;, &#39;Para 2&#39;,
<a name="line160"></a>               el.childNodes[1].innerHTML);
<a name="line161"></a>
<a name="line162"></a>  assertEquals(goog.dom.$dom, goog.dom.createDom);
<a name="line163"></a>}
<a name="line164"></a>
<a name="line165"></a>function testCreateDomNoChildren() {
<a name="line166"></a>  var el;
<a name="line167"></a>
<a name="line168"></a>  // Test unspecified children.
<a name="line169"></a>  el = goog.dom.$dom(&#39;div&#39;);
<a name="line170"></a>  assertNull(&#39;firstChild should be null&#39;, el.firstChild);
<a name="line171"></a>
<a name="line172"></a>  // Test null children.
<a name="line173"></a>  el = goog.dom.$dom(&#39;div&#39;, null, null);
<a name="line174"></a>  assertNull(&#39;firstChild should be null&#39;, el.firstChild);
<a name="line175"></a>
<a name="line176"></a>  // Test empty array of children.
<a name="line177"></a>  el = goog.dom.$dom(&#39;div&#39;, null, []);
<a name="line178"></a>  assertNull(&#39;firstChild should be null&#39;, el.firstChild);
<a name="line179"></a>}
<a name="line180"></a>
<a name="line181"></a>function testCreateDomAcceptsArray() {
<a name="line182"></a>  var items = [
<a name="line183"></a>    goog.dom.$dom(&#39;li&#39;, {}, &#39;Item 1&#39;),
<a name="line184"></a>    goog.dom.$dom(&#39;li&#39;, {}, &#39;Item 2&#39;)
<a name="line185"></a>  ];
<a name="line186"></a>  var ul = goog.dom.$dom(&#39;ul&#39;, {}, items);
<a name="line187"></a>  assertEquals(&#39;List should have two children&#39;, 2, ul.childNodes.length);
<a name="line188"></a>  assertEquals(&#39;First child should be an LI tag&#39;,
<a name="line189"></a>      &#39;LI&#39;, ul.firstChild.tagName);
<a name="line190"></a>  assertEquals(&#39;Item 1&#39;, ul.childNodes[0].innerHTML);
<a name="line191"></a>  assertEquals(&#39;Item 2&#39;, ul.childNodes[1].innerHTML);
<a name="line192"></a>}
<a name="line193"></a>
<a name="line194"></a>function testCreateDomStringArg() {
<a name="line195"></a>  var el;
<a name="line196"></a>
<a name="line197"></a>  // Test string arg.
<a name="line198"></a>  el = goog.dom.$dom(&#39;div&#39;, null, &#39;Hello&#39;);
<a name="line199"></a>  assertEquals(&#39;firstChild should be a text node&#39;, goog.dom.NodeType.TEXT,
<a name="line200"></a>      el.firstChild.nodeType);
<a name="line201"></a>  assertEquals(&#39;firstChild should have node value &quot;Hello&quot;&#39;, &#39;Hello&#39;,
<a name="line202"></a>      el.firstChild.nodeValue);
<a name="line203"></a>
<a name="line204"></a>  // Test text node arg.
<a name="line205"></a>  el = goog.dom.$dom(&#39;div&#39;, null, goog.dom.createTextNode(&#39;World&#39;));
<a name="line206"></a>  assertEquals(&#39;firstChild should be a text node&#39;, goog.dom.NodeType.TEXT,
<a name="line207"></a>      el.firstChild.nodeType);
<a name="line208"></a>  assertEquals(&#39;firstChild should have node value &quot;World&quot;&#39;, &#39;World&#39;,
<a name="line209"></a>      el.firstChild.nodeValue);
<a name="line210"></a>}
<a name="line211"></a>
<a name="line212"></a>function testCreateDomNodeListArg() {
<a name="line213"></a>  var el;
<a name="line214"></a>  var emptyElem = goog.dom.$dom(&#39;div&#39;);
<a name="line215"></a>  var simpleElem = goog.dom.$dom(&#39;div&#39;, null, &#39;Hello, world!&#39;);
<a name="line216"></a>  var complexElem = goog.dom.$dom(&#39;div&#39;, null, &#39;Hello, &#39;,
<a name="line217"></a>                                  goog.dom.$dom(&#39;b&#39;, null, &#39;world&#39;),
<a name="line218"></a>      goog.dom.createTextNode(&#39;!&#39;));
<a name="line219"></a>
<a name="line220"></a>  // Test empty node list.
<a name="line221"></a>  el = goog.dom.$dom(&#39;div&#39;, null, emptyElem.childNodes);
<a name="line222"></a>  assertNull(&#39;emptyElem.firstChild should be null&#39;, emptyElem.firstChild);
<a name="line223"></a>  assertNull(&#39;firstChild should be null&#39;, el.firstChild);
<a name="line224"></a>
<a name="line225"></a>  // Test simple node list.
<a name="line226"></a>  el = goog.dom.$dom(&#39;div&#39;, null, simpleElem.childNodes);
<a name="line227"></a>  assertNull(&#39;simpleElem.firstChild should be null&#39;, simpleElem.firstChild);
<a name="line228"></a>  assertEquals(&#39;firstChild should be a text node with value &quot;Hello, world!&quot;&#39;,
<a name="line229"></a>      &#39;Hello, world!&#39;, el.firstChild.nodeValue);
<a name="line230"></a>
<a name="line231"></a>  // Test complex node list.
<a name="line232"></a>  el = goog.dom.$dom(&#39;div&#39;, null, complexElem.childNodes);
<a name="line233"></a>  assertNull(&#39;complexElem.firstChild should be null&#39;, complexElem.firstChild);
<a name="line234"></a>  assertEquals(&#39;Element should have 3 child nodes&#39;, 3, el.childNodes.length);
<a name="line235"></a>  assertEquals(&#39;childNodes[0] should be a text node with value &quot;Hello, &quot;&#39;,
<a name="line236"></a>      &#39;Hello, &#39;, el.childNodes[0].nodeValue);
<a name="line237"></a>  assertEquals(&#39;childNodes[1] should be an element node with tagName &quot;B&quot;&#39;,
<a name="line238"></a>      &#39;B&#39;, el.childNodes[1].tagName);
<a name="line239"></a>  assertEquals(&#39;childNodes[2] should be a text node with value &quot;!&quot;&#39;, &#39;!&#39;,
<a name="line240"></a>      el.childNodes[2].nodeValue);
<a name="line241"></a>}
<a name="line242"></a>
<a name="line243"></a>function testCreateDomWithTypeAttribute() {
<a name="line244"></a>  var el = goog.dom.createDom(&#39;button&#39;, {&#39;type&#39;: &#39;reset&#39;}, &#39;Cool button&#39;);
<a name="line245"></a>  assertNotNull(&#39;Button with type attribute was created successfully&#39;, el);
<a name="line246"></a>  assertEquals(&#39;Button has correct type attribute&#39;, &#39;reset&#39;, el.type);
<a name="line247"></a>}
<a name="line248"></a>
<a name="line249"></a>function testContains() {
<a name="line250"></a>  assertTrue(&#39;HTML should contain BODY&#39;, goog.dom.contains(
<a name="line251"></a>      document.documentElement, document.body));
<a name="line252"></a>  assertTrue(&#39;Document should contain BODY&#39;, goog.dom.contains(
<a name="line253"></a>      document, document.body));
<a name="line254"></a>
<a name="line255"></a>  var d = goog.dom.$dom(&#39;p&#39;, null, &#39;A paragraph&#39;);
<a name="line256"></a>  var t = d.firstChild;
<a name="line257"></a>  assertTrue(&#39;Same element&#39;, goog.dom.contains(d, d));
<a name="line258"></a>  assertTrue(&#39;Same text&#39;, goog.dom.contains(t, t));
<a name="line259"></a>  assertTrue(&#39;Nested text&#39;, goog.dom.contains(d, t));
<a name="line260"></a>  assertFalse(&#39;Nested text, reversed&#39;, goog.dom.contains(t, d));
<a name="line261"></a>  assertFalse(&#39;Disconnected element&#39;, goog.dom.contains(
<a name="line262"></a>      document, d));
<a name="line263"></a>  goog.dom.appendChild(document.body, d);
<a name="line264"></a>  assertTrue(&#39;Connected element&#39;, goog.dom.contains(
<a name="line265"></a>      document, d));
<a name="line266"></a>  goog.dom.removeNode(d);
<a name="line267"></a>}
<a name="line268"></a>
<a name="line269"></a>function testCreateDomWithClassName() {
<a name="line270"></a>  var el = goog.dom.$dom(&#39;div&#39;, &#39;cls&#39;);
<a name="line271"></a>  assertNull(&#39;firstChild should be null&#39;, el.firstChild);
<a name="line272"></a>  assertEquals(&#39;Tagname should be a DIV&#39;, &#39;DIV&#39;, el.tagName);
<a name="line273"></a>  assertEquals(&#39;ClassName should be cls&#39;, &#39;cls&#39;, el.className);
<a name="line274"></a>
<a name="line275"></a>  el = goog.dom.$dom(&#39;div&#39;, &#39;&#39;);
<a name="line276"></a>  assertEquals(&#39;ClassName should be empty&#39;, &#39;&#39;, el.className);
<a name="line277"></a>}
<a name="line278"></a>
<a name="line279"></a>function testCompareNodeOrder() {
<a name="line280"></a>  var b1 = $(&#39;b1&#39;);
<a name="line281"></a>  var b2 = $(&#39;b2&#39;);
<a name="line282"></a>  var p2 = $(&#39;p2&#39;);
<a name="line283"></a>
<a name="line284"></a>  assertEquals(&#39;equal nodes should compare to 0&#39;, 0,
<a name="line285"></a>      goog.dom.compareNodeOrder(b1, b1));
<a name="line286"></a>
<a name="line287"></a>  assertTrue(&#39;parent should come before child&#39;,
<a name="line288"></a>      goog.dom.compareNodeOrder(p2, b1) &lt; 0);
<a name="line289"></a>  assertTrue(&#39;child should come after parent&#39;,
<a name="line290"></a>      goog.dom.compareNodeOrder(b1, p2) &gt; 0);
<a name="line291"></a>
<a name="line292"></a>  assertTrue(&#39;parent should come before text child&#39;,
<a name="line293"></a>      goog.dom.compareNodeOrder(b1, b1.firstChild) &lt; 0);
<a name="line294"></a>  assertTrue(&#39;text child should come after parent&#39;, goog.dom.compareNodeOrder(
<a name="line295"></a>      b1.firstChild, b1) &gt; 0);
<a name="line296"></a>
<a name="line297"></a>  assertTrue(&#39;first sibling should come before second&#39;,
<a name="line298"></a>      goog.dom.compareNodeOrder(b1, b2) &lt; 0);
<a name="line299"></a>  assertTrue(&#39;second sibling should come after first&#39;,
<a name="line300"></a>      goog.dom.compareNodeOrder(b2, b1) &gt; 0);
<a name="line301"></a>
<a name="line302"></a>  assertTrue(&#39;text node after cousin element returns correct value&#39;,
<a name="line303"></a>      goog.dom.compareNodeOrder(b1.nextSibling, b1) &gt; 0);
<a name="line304"></a>  assertTrue(&#39;text node before cousin element returns correct value&#39;,
<a name="line305"></a>      goog.dom.compareNodeOrder(b1, b1.nextSibling) &lt; 0);
<a name="line306"></a>
<a name="line307"></a>  assertTrue(&#39;text node is before once removed cousin element&#39;,
<a name="line308"></a>      goog.dom.compareNodeOrder(b1.firstChild, b2) &lt; 0);
<a name="line309"></a>  assertTrue(&#39;once removed cousin element is before text node&#39;,
<a name="line310"></a>      goog.dom.compareNodeOrder(b2, b1.firstChild) &gt; 0);
<a name="line311"></a>
<a name="line312"></a>  assertTrue(&#39;text node is after once removed cousin text node&#39;,
<a name="line313"></a>      goog.dom.compareNodeOrder(b1.nextSibling, b1.firstChild) &gt; 0);
<a name="line314"></a>  assertTrue(&#39;once removed cousin text node is before text node&#39;,
<a name="line315"></a>      goog.dom.compareNodeOrder(b1.firstChild, b1.nextSibling) &lt; 0);
<a name="line316"></a>
<a name="line317"></a>  assertTrue(&#39;first text node is before second text node&#39;,
<a name="line318"></a>      goog.dom.compareNodeOrder(b1.previousSibling, b1.nextSibling) &lt; 0);
<a name="line319"></a>  assertTrue(&#39;second text node is after first text node&#39;,
<a name="line320"></a>      goog.dom.compareNodeOrder(b1.nextSibling, b1.previousSibling) &gt; 0);
<a name="line321"></a>
<a name="line322"></a>  assertTrue(&#39;grandchild is after grandparent&#39;,
<a name="line323"></a>      goog.dom.compareNodeOrder(b1.firstChild, b1.parentNode) &gt; 0);
<a name="line324"></a>  assertTrue(&#39;grandparent is after grandchild&#39;,
<a name="line325"></a>      goog.dom.compareNodeOrder(b1.parentNode, b1.firstChild) &lt; 0);
<a name="line326"></a>
<a name="line327"></a>  assertTrue(&#39;grandchild is after grandparent&#39;,
<a name="line328"></a>      goog.dom.compareNodeOrder(b1.firstChild, b1.parentNode) &gt; 0);
<a name="line329"></a>  assertTrue(&#39;grandparent is after grandchild&#39;,
<a name="line330"></a>      goog.dom.compareNodeOrder(b1.parentNode, b1.firstChild) &lt; 0);
<a name="line331"></a>
<a name="line332"></a>  assertTrue(&#39;second cousins compare correctly&#39;,
<a name="line333"></a>      goog.dom.compareNodeOrder(b1.firstChild, b2.firstChild) &lt; 0);
<a name="line334"></a>  assertTrue(&#39;second cousins compare correctly in reverse&#39;,
<a name="line335"></a>      goog.dom.compareNodeOrder(b2.firstChild, b1.firstChild) &gt; 0);
<a name="line336"></a>
<a name="line337"></a>  assertTrue(&#39;testEl2 is after testEl&#39;,
<a name="line338"></a>      goog.dom.compareNodeOrder($(&#39;testEl2&#39;), $(&#39;testEl&#39;)) &gt; 0);
<a name="line339"></a>  assertTrue(&#39;testEl is before testEl2&#39;,
<a name="line340"></a>      goog.dom.compareNodeOrder($(&#39;testEl&#39;), $(&#39;testEl2&#39;)) &lt; 0);
<a name="line341"></a>
<a name="line342"></a>  var p = $(&#39;order-test&#39;);
<a name="line343"></a>  var text1 = document.createTextNode(&#39;1&#39;);
<a name="line344"></a>  p.appendChild(text1);
<a name="line345"></a>  var text2 = document.createTextNode(&#39;1&#39;);
<a name="line346"></a>  p.appendChild(text2);
<a name="line347"></a>
<a name="line348"></a>  assertEquals(&#39;Equal text nodes should compare to 0&#39;, 0,
<a name="line349"></a>      goog.dom.compareNodeOrder(text1, text1));
<a name="line350"></a>  assertTrue(&#39;First text node is before second&#39;,
<a name="line351"></a>      goog.dom.compareNodeOrder(text1, text2) &lt; 0);
<a name="line352"></a>  assertTrue(&#39;Second text node is after first&#39;,
<a name="line353"></a>      goog.dom.compareNodeOrder(text2, text1) &gt; 0);
<a name="line354"></a>  assertTrue(&#39;Late text node is after b1&#39;,
<a name="line355"></a>      goog.dom.compareNodeOrder(text1, $(&#39;b1&#39;)) &gt; 0);
<a name="line356"></a>}
<a name="line357"></a>
<a name="line358"></a>function testFindCommonAncestor() {
<a name="line359"></a>  var b1 = $(&#39;b1&#39;);
<a name="line360"></a>  var b2 = $(&#39;b2&#39;);
<a name="line361"></a>  var p1 = $(&#39;p1&#39;);
<a name="line362"></a>  var p2 = $(&#39;p2&#39;);
<a name="line363"></a>  var testEl2 = $(&#39;testEl2&#39;);
<a name="line364"></a>
<a name="line365"></a>  assertNull(&#39;findCommonAncestor() = null&#39;, goog.dom.findCommonAncestor());
<a name="line366"></a>  assertEquals(&#39;findCommonAncestor(b1) = b1&#39;, b1,
<a name="line367"></a>      goog.dom.findCommonAncestor(b1));
<a name="line368"></a>  assertEquals(&#39;findCommonAncestor(b1, b1) = b1&#39;, b1,
<a name="line369"></a>      goog.dom.findCommonAncestor(b1, b1));
<a name="line370"></a>  assertEquals(&#39;findCommonAncestor(b1, b2) = p2&#39;, p2,
<a name="line371"></a>      goog.dom.findCommonAncestor(b1, b2));
<a name="line372"></a>  assertEquals(&#39;findCommonAncestor(p1, b2) = body&#39;, document.body,
<a name="line373"></a>      goog.dom.findCommonAncestor(p1, b2));
<a name="line374"></a>  assertEquals(&#39;findCommonAncestor(testEl2, b1, b2, p1, p2) = body&#39;,
<a name="line375"></a>      document.body, goog.dom.findCommonAncestor(testEl2, b1, b2, p1, p2));
<a name="line376"></a>
<a name="line377"></a>  var outOfDoc = document.createElement(&#39;div&#39;);
<a name="line378"></a>  assertNull(&#39;findCommonAncestor(outOfDoc, b1) = null&#39;,
<a name="line379"></a>      goog.dom.findCommonAncestor(outOfDoc, b1));
<a name="line380"></a>}
<a name="line381"></a>
<a name="line382"></a>function testRemoveNode() {
<a name="line383"></a>  var b = document.createElement(&#39;b&#39;);
<a name="line384"></a>  var el = $(&#39;p1&#39;);
<a name="line385"></a>  el.appendChild(b);
<a name="line386"></a>  goog.dom.removeNode(b);
<a name="line387"></a>  assertTrue(&#39;b should have been removed&#39;, el.lastChild != b);
<a name="line388"></a>}
<a name="line389"></a>
<a name="line390"></a>function testReplaceNode() {
<a name="line391"></a>  var n = $(&#39;toReplace&#39;);
<a name="line392"></a>  var previousSibling = n.previousSibling;
<a name="line393"></a>  var goodNode = goog.dom.createDom(&#39;div&#39;, {&#39;id&#39;: &#39;goodReplaceNode&#39;});
<a name="line394"></a>  goog.dom.replaceNode(goodNode, n);
<a name="line395"></a>
<a name="line396"></a>  assertEquals(&#39;n should have been replaced&#39;, previousSibling.nextSibling,
<a name="line397"></a>      goodNode);
<a name="line398"></a>  assertNull(&#39;n should no longer be in the DOM tree&#39;, $(&#39;toReplace&#39;));
<a name="line399"></a>
<a name="line400"></a>  var badNode = goog.dom.createDom(&#39;div&#39;, {&#39;id&#39;: &#39;badReplaceNode&#39;});
<a name="line401"></a>  goog.dom.replaceNode(badNode, n);
<a name="line402"></a>  assertNull(&#39;badNode should not be in the DOM tree&#39;, $(&#39;badReplaceNode&#39;));
<a name="line403"></a>}
<a name="line404"></a>
<a name="line405"></a>function testFlattenElement() {
<a name="line406"></a>  var text = document.createTextNode(&#39;Text&#39;);
<a name="line407"></a>  var br = document.createElement(&#39;br&#39;);
<a name="line408"></a>  var span = goog.dom.createDom(&#39;span&#39;, null, text, br);
<a name="line409"></a>  assertEquals(&#39;span should have 2 children&#39;, 2, span.childNodes.length);
<a name="line410"></a>
<a name="line411"></a>  var el = $(&#39;p1&#39;);
<a name="line412"></a>  el.appendChild(span);
<a name="line413"></a>
<a name="line414"></a>  var ret = goog.dom.flattenElement(span);
<a name="line415"></a>
<a name="line416"></a>  assertTrue(&#39;span should have been removed&#39;, el.lastChild != span);
<a name="line417"></a>  assertFalse(&#39;span should have no parent&#39;, !!span.parentNode &amp;&amp;
<a name="line418"></a>      span.parentNode.nodeType != goog.dom.NodeType.DOCUMENT_FRAGMENT);
<a name="line419"></a>  assertEquals(&#39;span should have no children&#39;, 0, span.childNodes.length);
<a name="line420"></a>  assertEquals(&#39;Last child of p should be br&#39;, br, el.lastChild);
<a name="line421"></a>  assertEquals(&#39;Previous sibling of br should be text&#39;, text,
<a name="line422"></a>      br.previousSibling);
<a name="line423"></a>
<a name="line424"></a>  var outOfDoc = goog.dom.createDom(&#39;span&#39;, null, &#39;1 child&#39;);
<a name="line425"></a>  // Should do nothing.
<a name="line426"></a>  goog.dom.flattenElement(outOfDoc);
<a name="line427"></a>  assertEquals(&#39;outOfDoc should still have 1 child&#39;, 1,
<a name="line428"></a>      outOfDoc.childNodes.length);
<a name="line429"></a>}
<a name="line430"></a>
<a name="line431"></a>function testIsNodeLike() {
<a name="line432"></a>  assertTrue(&#39;document should be node like&#39;, goog.dom.isNodeLike(document));
<a name="line433"></a>  assertTrue(&#39;document.body should be node like&#39;,
<a name="line434"></a>             goog.dom.isNodeLike(document.body));
<a name="line435"></a>  assertTrue(&#39;a text node should be node like&#39;, goog.dom.isNodeLike(
<a name="line436"></a>      document.createTextNode(&#39;&#39;)));
<a name="line437"></a>
<a name="line438"></a>  assertFalse(&#39;null should not be node like&#39;, goog.dom.isNodeLike(null));
<a name="line439"></a>  assertFalse(&#39;a string should not be node like&#39;, goog.dom.isNodeLike(&#39;abcd&#39;));
<a name="line440"></a>
<a name="line441"></a>  assertTrue(&#39;custom object should be node like&#39;,
<a name="line442"></a>             goog.dom.isNodeLike({nodeType: 1}));
<a name="line443"></a>}
<a name="line444"></a>
<a name="line445"></a>function testGetOwnerDocument() {
<a name="line446"></a>  assertEquals(goog.dom.getOwnerDocument($(&#39;p1&#39;)), document);
<a name="line447"></a>  assertEquals(goog.dom.getOwnerDocument(document.body), document);
<a name="line448"></a>  assertEquals(goog.dom.getOwnerDocument(document.documentElement), document);
<a name="line449"></a>}
<a name="line450"></a>
<a name="line451"></a>function testDomHelper() {
<a name="line452"></a>  var x = new goog.dom.DomHelper(window.frames[&#39;frame&#39;].document);
<a name="line453"></a>  assertTrue(&#39;Should have some HTML&#39;,
<a name="line454"></a>             x.getDocument().body.innerHTML.length &gt; 0);
<a name="line455"></a>}
<a name="line456"></a>
<a name="line457"></a>function testGetFirstElementChild() {
<a name="line458"></a>  var p2 = $(&#39;p2&#39;);
<a name="line459"></a>  var b1 = goog.dom.getFirstElementChild(p2);
<a name="line460"></a>  assertNotNull(&#39;First element child of p2 should not be null&#39;, b1);
<a name="line461"></a>  assertEquals(&#39;First element child is b1&#39;, &#39;b1&#39;, b1.id);
<a name="line462"></a>
<a name="line463"></a>  var c = goog.dom.getFirstElementChild(b1);
<a name="line464"></a>  assertNull(&#39;First element child of b1 should be null&#39;, c);
<a name="line465"></a>}
<a name="line466"></a>
<a name="line467"></a>function testGetLastElementChild() {
<a name="line468"></a>  var p2 = $(&#39;p2&#39;);
<a name="line469"></a>  var b2 = goog.dom.getLastElementChild(p2);
<a name="line470"></a>  assertNotNull(&#39;Last element child of p2 should not be null&#39;, b2);
<a name="line471"></a>  assertEquals(&#39;Last element child is b2&#39;, &#39;b2&#39;, b2.id);
<a name="line472"></a>
<a name="line473"></a>  var c = goog.dom.getLastElementChild(b2);
<a name="line474"></a>  assertNull(&#39;Last element child of b2 should be null&#39;, c);
<a name="line475"></a>}
<a name="line476"></a>
<a name="line477"></a>function testGetNextElementSibling() {
<a name="line478"></a>  var b1 = $(&#39;b1&#39;);
<a name="line479"></a>  var b2 = goog.dom.getNextElementSibling(b1);
<a name="line480"></a>  assertNotNull(&#39;Next element sibling of b1 should not be null&#39;, b1);
<a name="line481"></a>  assertEquals(&#39;Next element sibling is b2&#39;, &#39;b2&#39;, b2.id);
<a name="line482"></a>
<a name="line483"></a>  var c = goog.dom.getNextElementSibling(b2);
<a name="line484"></a>  assertNull(&#39;Next element sibling of b2 should be null&#39;, c);
<a name="line485"></a>}
<a name="line486"></a>
<a name="line487"></a>function testGetPreviousElementSibling() {
<a name="line488"></a>  var b2 = $(&#39;b2&#39;);
<a name="line489"></a>  var b1 = goog.dom.getPreviousElementSibling(b2);
<a name="line490"></a>  assertNotNull(&#39;Previous element sibling of b2 should not be null&#39;, b1);
<a name="line491"></a>  assertEquals(&#39;Previous element sibling is b1&#39;, &#39;b1&#39;, b1.id);
<a name="line492"></a>
<a name="line493"></a>  var c = goog.dom.getPreviousElementSibling(b1);
<a name="line494"></a>  assertNull(&#39;Previous element sibling of b1 should be null&#39;, c);
<a name="line495"></a>}
<a name="line496"></a>
<a name="line497"></a>function testSetTextContent() {
<a name="line498"></a>  var p1 = $(&#39;p1&#39;);
<a name="line499"></a>  var s = &#39;hello world&#39;;
<a name="line500"></a>  goog.dom.setTextContent(p1, s);
<a name="line501"></a>  assertEquals(&#39;We should have one childNode after setTextContent&#39;, 1,
<a name="line502"></a>      p1.childNodes.length);
<a name="line503"></a>  assertEquals(s, p1.firstChild.data);
<a name="line504"></a>  assertEquals(s, p1.innerHTML);
<a name="line505"></a>
<a name="line506"></a>  s = &#39;four elefants &lt; five ants&#39;;
<a name="line507"></a>  var sHtml = &#39;four elefants &amp;lt; five ants&#39;;
<a name="line508"></a>  goog.dom.setTextContent(p1, s);
<a name="line509"></a>  assertEquals(&#39;We should have one childNode after setTextContent&#39;, 1,
<a name="line510"></a>      p1.childNodes.length);
<a name="line511"></a>  assertEquals(s, p1.firstChild.data);
<a name="line512"></a>  assertEquals(sHtml, p1.innerHTML);
<a name="line513"></a>
<a name="line514"></a>  // ensure that we remove existing children
<a name="line515"></a>  p1.innerHTML = &#39;a&lt;b&gt;b&lt;/b&gt;c&#39;;
<a name="line516"></a>  s = &#39;hello world&#39;;
<a name="line517"></a>  goog.dom.setTextContent(p1, s);
<a name="line518"></a>  assertEquals(&#39;We should have one childNode after setTextContent&#39;, 1,
<a name="line519"></a>      p1.childNodes.length);
<a name="line520"></a>  assertEquals(s, p1.firstChild.data);
<a name="line521"></a>
<a name="line522"></a>  // same but start with an element
<a name="line523"></a>  p1.innerHTML = &#39;&lt;b&gt;a&lt;/b&gt;b&lt;i&gt;c&lt;/i&gt;&#39;;
<a name="line524"></a>  s = &#39;hello world&#39;;
<a name="line525"></a>  goog.dom.setTextContent(p1, s);
<a name="line526"></a>  assertEquals(&#39;We should have one childNode after setTextContent&#39;, 1,
<a name="line527"></a>      p1.childNodes.length);
<a name="line528"></a>  assertEquals(s, p1.firstChild.data);
<a name="line529"></a>
<a name="line530"></a>  // clean up
<a name="line531"></a>  p1.innerHTML = &#39;&#39;;
<a name="line532"></a>}
<a name="line533"></a>
<a name="line534"></a>function testFindNode() {
<a name="line535"></a>  var expected = document.body;
<a name="line536"></a>  var result = goog.dom.findNode(document, function(n) {
<a name="line537"></a>    return n.nodeType == goog.dom.NodeType.ELEMENT &amp;&amp; n.tagName == &#39;BODY&#39;;
<a name="line538"></a>  });
<a name="line539"></a>  assertEquals(expected, result);
<a name="line540"></a>
<a name="line541"></a>  expected = document.getElementsByTagName(&#39;P&#39;)[0];
<a name="line542"></a>  result = goog.dom.findNode(document, function(n) {
<a name="line543"></a>    return n.nodeType == goog.dom.NodeType.ELEMENT &amp;&amp; n.tagName == &#39;P&#39;;
<a name="line544"></a>  });
<a name="line545"></a>  assertEquals(expected, result);
<a name="line546"></a>
<a name="line547"></a>  result = goog.dom.findNode(document, function(n) {
<a name="line548"></a>    return false;
<a name="line549"></a>  });
<a name="line550"></a>  assertUndefined(result);
<a name="line551"></a>}
<a name="line552"></a>
<a name="line553"></a>function testFindNodes() {
<a name="line554"></a>  var expected = document.getElementsByTagName(&#39;P&#39;);
<a name="line555"></a>  var result = goog.dom.findNodes(document, function(n) {
<a name="line556"></a>    return n.nodeType == goog.dom.NodeType.ELEMENT &amp;&amp; n.tagName == &#39;P&#39;;
<a name="line557"></a>  });
<a name="line558"></a>  assertEquals(expected.length, result.length);
<a name="line559"></a>  assertEquals(expected[0], result[0]);
<a name="line560"></a>  assertEquals(expected[1], result[1]);
<a name="line561"></a>
<a name="line562"></a>  result = goog.dom.findNodes(document, function(n) {
<a name="line563"></a>    return false;
<a name="line564"></a>  }).length;
<a name="line565"></a>  assertEquals(0, result);
<a name="line566"></a>}
<a name="line567"></a>
<a name="line568"></a>function createTestDom(txt) {
<a name="line569"></a>  var dom = goog.dom.createDom(&#39;div&#39;);
<a name="line570"></a>  dom.innerHTML = txt;
<a name="line571"></a>  return dom;
<a name="line572"></a>}
<a name="line573"></a>
<a name="line574"></a>function testIsFocusableTabIndex() {
<a name="line575"></a>  assertFalse(&#39;isFocusableTabIndex() must be false for no tab index&#39;,
<a name="line576"></a>      goog.dom.isFocusableTabIndex(goog.dom.$(&#39;noTabIndex&#39;)));
<a name="line577"></a>  assertFalse(&#39;isFocusableTabIndex() must be false for tab index -2&#39;,
<a name="line578"></a>      goog.dom.isFocusableTabIndex(goog.dom.$(&#39;tabIndexNegative2&#39;)));
<a name="line579"></a>  assertFalse(&#39;isFocusableTabIndex() must be false for tab index -1&#39;,
<a name="line580"></a>      goog.dom.isFocusableTabIndex(goog.dom.$(&#39;tabIndexNegative1&#39;)));
<a name="line581"></a>
<a name="line582"></a>  // WebKit on Mac doesn&#39;t support focusable DIVs until version 526 and later.
<a name="line583"></a>  if (!goog.userAgent.WEBKIT || !goog.userAgent.MAC ||
<a name="line584"></a>      goog.userAgent.isVersion(&#39;526&#39;)) {
<a name="line585"></a>    assertTrue(&#39;isFocusableTabIndex() must be true for tab index 0&#39;,
<a name="line586"></a>        goog.dom.isFocusableTabIndex(goog.dom.$(&#39;tabIndex0&#39;)));
<a name="line587"></a>    assertTrue(&#39;isFocusableTabIndex() must be true for tab index 1&#39;,
<a name="line588"></a>        goog.dom.isFocusableTabIndex(goog.dom.$(&#39;tabIndex1&#39;)));
<a name="line589"></a>    assertTrue(&#39;isFocusableTabIndex() must be true for tab index 2&#39;,
<a name="line590"></a>        goog.dom.isFocusableTabIndex(goog.dom.$(&#39;tabIndex2&#39;)));
<a name="line591"></a>  }
<a name="line592"></a>}
<a name="line593"></a>
<a name="line594"></a>function testSetFocusableTabIndex() {
<a name="line595"></a>  // WebKit on Mac doesn&#39;t support focusable DIVs until version 526 and later.
<a name="line596"></a>  if (!goog.userAgent.WEBKIT || !goog.userAgent.MAC ||
<a name="line597"></a>      goog.userAgent.isVersion(&#39;526&#39;)) {
<a name="line598"></a>    // Test enabling focusable tab index.
<a name="line599"></a>    goog.dom.setFocusableTabIndex(goog.dom.$(&#39;noTabIndex&#39;), true);
<a name="line600"></a>    assertTrue(&#39;isFocusableTabIndex() must be true after enabling tab index&#39;,
<a name="line601"></a>        goog.dom.isFocusableTabIndex(goog.dom.$(&#39;noTabIndex&#39;)));
<a name="line602"></a>
<a name="line603"></a>    // Test disabling focusable tab index that was added programmatically.
<a name="line604"></a>    goog.dom.setFocusableTabIndex(goog.dom.$(&#39;noTabIndex&#39;), false);
<a name="line605"></a>    assertFalse(&#39;isFocusableTabIndex() must be false after disabling tab &#39; +
<a name="line606"></a>        &#39;index that was programmatically added&#39;,
<a name="line607"></a>        goog.dom.isFocusableTabIndex(goog.dom.$(&#39;noTabIndex&#39;)));
<a name="line608"></a>
<a name="line609"></a>    // Test disabling focusable tab index that was specified in markup.
<a name="line610"></a>    goog.dom.setFocusableTabIndex(goog.dom.$(&#39;tabIndex0&#39;), false);
<a name="line611"></a>    assertFalse(&#39;isFocusableTabIndex() must be false after disabling tab &#39; +
<a name="line612"></a>        &#39;index that was specified in markup&#39;,
<a name="line613"></a>        goog.dom.isFocusableTabIndex(goog.dom.$(&#39;tabIndex0&#39;)));
<a name="line614"></a>
<a name="line615"></a>    // Test re-enabling focusable tab index.
<a name="line616"></a>    goog.dom.setFocusableTabIndex(goog.dom.$(&#39;tabIndex0&#39;), true);
<a name="line617"></a>    assertTrue(&#39;isFocusableTabIndex() must be true after reenabling tab&#39; +
<a name="line618"></a>        &#39;index&#39;, goog.dom.isFocusableTabIndex(goog.dom.$(&#39;tabIndex0&#39;)));
<a name="line619"></a>  }
<a name="line620"></a>}
<a name="line621"></a>
<a name="line622"></a>function testGetTextContent() {
<a name="line623"></a>  function t(inp, out) {
<a name="line624"></a>    assertEquals(out.replace(/ /g, &#39;_&#39;),
<a name="line625"></a>                 goog.dom.getTextContent(
<a name="line626"></a>                    createTestDom(inp)).replace(/ /g, &#39;_&#39;));
<a name="line627"></a>  }
<a name="line628"></a>
<a name="line629"></a>  t(&#39;abcde&#39;, &#39;abcde&#39;);
<a name="line630"></a>  t(&#39;a&lt;b&gt;bcd&lt;/b&gt;efgh&#39;, &#39;abcdefgh&#39;);
<a name="line631"></a>  t(&#39;a&lt;script type=&quot;text/javascript&#39; + &#39;&quot;&gt;var a=1;&lt;&#39; + &#39;/script&gt;h&#39;, &#39;ah&#39;);
<a name="line632"></a>  t(&#39;&lt;html&gt;&lt;head&gt;&lt;style type=&quot;text/css&quot;&gt;&#39; +
<a name="line633"></a>    &#39;p{margin:100%;padding:5px}\n.class{background-color:red;}&lt;/style&gt;&#39; +
<a name="line634"></a>    &#39;&lt;/head&gt;&lt;body&gt;&lt;h1&gt;Hello&lt;/h1&gt;\n&lt;p&gt;One two three&lt;/p&gt;\n&lt;table&gt;&lt;tr&gt;&lt;td&gt;a&#39; +
<a name="line635"></a>    &#39;&lt;td&gt;b&lt;/table&gt;&lt;&#39; + &#39;script&gt;var a = \&#39;foo\&#39;;&#39; +
<a name="line636"></a>    &#39;&lt;/scrip&#39; + &#39;t&gt;&lt;/body&gt;&lt;/html&gt;&#39;, &#39;HelloOne two threeab&#39;);
<a name="line637"></a>  t(&#39;abc&lt;br&gt;def&#39;, &#39;abc\ndef&#39;);
<a name="line638"></a>  t(&#39;abc&lt;br&gt;\ndef&#39;, &#39;abc\ndef&#39;);
<a name="line639"></a>  t(&#39;abc&lt;br&gt;\n\ndef&#39;, &#39;abc\ndef&#39;);
<a name="line640"></a>  t(&#39;abc&lt;br&gt;&lt;br&gt;\ndef&#39;, &#39;abc\n\ndef&#39;);
<a name="line641"></a>  t(&#39; &lt;b&gt;abcde  &lt;/b&gt;   &#39;, &#39;abcde &#39;);
<a name="line642"></a>  t(&#39; &lt;b&gt;abcde    &lt;/b&gt; hi  &#39;, &#39;abcde hi &#39;);
<a name="line643"></a>  t(&#39; \n&lt;b&gt;abcde  &lt;/b&gt;   &#39;, &#39;abcde &#39;);
<a name="line644"></a>  t(&#39; \n&lt;b&gt;abcde  &lt;/b&gt;   \n\n\n&#39;, &#39;abcde &#39;);
<a name="line645"></a>  t(&#39;&lt;p&gt;abcde&lt;/p&gt;\nfg&#39;, &#39;abcdefg&#39;);
<a name="line646"></a>  t(&#39;\n &lt;div&gt;  &lt;b&gt;abcde  &lt;/b&gt;   &#39;, &#39;abcde &#39;);
<a name="line647"></a>  t(&#39; \n&amp;shy;&lt;b&gt;abcde &amp;shy; &lt;/b&gt;   \n\n\n&amp;shy;&#39;, &#39;abcde &#39;);
<a name="line648"></a>  t(&#39; \n&amp;shy;\n\n&amp;shy;\na   &#39;, &#39;a &#39;);
<a name="line649"></a>  t(&#39; \n&lt;wbr&gt;&lt;/wbr&gt;&lt;b&gt;abcde &lt;wbr&gt;&lt;/wbr&gt; &lt;/b&gt;   \n\n\n&lt;wbr&gt;&lt;/wbr&gt;&#39;, &#39;abcde &#39;);
<a name="line650"></a>}
<a name="line651"></a>
<a name="line652"></a>function testGetNodeTextLength() {
<a name="line653"></a>
<a name="line654"></a>  assertEquals(6, goog.dom.getNodeTextLength(createTestDom(&#39;abcdef&#39;)));
<a name="line655"></a>  assertEquals(8, goog.dom.getNodeTextLength(
<a name="line656"></a>      createTestDom(&#39;a&lt;b&gt;bcd&lt;/b&gt;efgh&#39;)));
<a name="line657"></a>  assertEquals(2, goog.dom.getNodeTextLength(createTestDom(
<a name="line658"></a>      &#39;a&lt;script type=&quot;text/javascript&#39; + &#39;&quot;&gt;var a = 1234;&lt;&#39; + &#39;/script&gt;h&#39;)));
<a name="line659"></a>  assertEquals(4, goog.dom.getNodeTextLength(createTestDom(
<a name="line660"></a>      &#39;a&lt;br&gt;\n&lt;!-- some comments --&gt;\nfo&#39;)));
<a name="line661"></a>  assertEquals(20, goog.dom.getNodeTextLength(createTestDom(
<a name="line662"></a>      &#39;&lt;html&gt;&lt;head&gt;&lt;style type=&quot;text/css&quot;&gt;&#39; +
<a name="line663"></a>      &#39;p{margin:100%;padding:5px}\n.class{background-color:red;}&lt;/style&gt;&#39; +
<a name="line664"></a>      &#39;&lt;/head&gt;&lt;body&gt;&lt;h1&gt;Hello&lt;/h1&gt;&lt;p&gt;One two three&lt;/p&gt;&lt;table&gt;&lt;tr&gt;&lt;td&gt;a&lt;td&gt;b&#39; +
<a name="line665"></a>      &#39;&lt;/table&gt;&lt;&#39; + &#39;script&gt;var a = \&#39;foo\&#39;;&lt;/scrip&#39; +
<a name="line666"></a>      &#39;t&gt;&lt;/body&gt;&lt;/html&gt;&#39;)));
<a name="line667"></a>  assertEquals(10, goog.dom.getNodeTextLength(createTestDom(
<a name="line668"></a>      &#39;a&lt;b&gt;bcd&lt;/b&gt;&lt;br /&gt;efghi&#39;)));
<a name="line669"></a>}
<a name="line670"></a>
<a name="line671"></a>function testGetNodeTextOffset() {
<a name="line672"></a>  assertEquals(4, goog.dom.getNodeTextOffset($(&#39;offsetTest1&#39;),
<a name="line673"></a>                                             $(&#39;offsetParent1&#39;)));
<a name="line674"></a>  assertEquals(12, goog.dom.getNodeTextOffset($(&#39;offsetTest1&#39;)));
<a name="line675"></a>}
<a name="line676"></a>
<a name="line677"></a>function testGetNodeAtOffset() {
<a name="line678"></a>  var html = &#39;&lt;div id=a&gt;123&lt;b id=b&gt;45&lt;/b&gt;&lt;span id=c&gt;67&lt;b id=d&gt;89&lt;i id=e&gt;01&#39; +
<a name="line679"></a>             &#39;&lt;/i&gt;23&lt;i id=f&gt;45&lt;/i&gt;67&lt;/b&gt;890&lt;i id=g&gt;123&lt;/i&gt;&lt;b id=h&gt;456&lt;/b&gt;&#39; +
<a name="line680"></a>             &#39;&lt;/span&gt;&lt;/div&gt;&lt;div id=i&gt;7890&lt;i id=j&gt;123&lt;/i&gt;&lt;/div&gt;&#39;;
<a name="line681"></a>  var node = document.createElement(&#39;div&#39;);
<a name="line682"></a>  node.innerHTML = html;
<a name="line683"></a>  var rv = {};
<a name="line684"></a>
<a name="line685"></a>  goog.dom.getNodeAtOffset(node, 2, rv);
<a name="line686"></a>  assertEquals(&#39;123&#39;, rv.node.nodeValue);
<a name="line687"></a>  assertEquals(&#39;a&#39;, rv.node.parentNode.id);
<a name="line688"></a>  assertEquals(1, rv.remainder);
<a name="line689"></a>
<a name="line690"></a>  goog.dom.getNodeAtOffset(node, 3, rv);
<a name="line691"></a>  assertEquals(&#39;123&#39;, rv.node.nodeValue);
<a name="line692"></a>  assertEquals(&#39;a&#39;, rv.node.parentNode.id);
<a name="line693"></a>  assertEquals(2, rv.remainder);
<a name="line694"></a>
<a name="line695"></a>  goog.dom.getNodeAtOffset(node, 5, rv);
<a name="line696"></a>  assertEquals(&#39;45&#39;, rv.node.nodeValue);
<a name="line697"></a>  assertEquals(&#39;b&#39;, rv.node.parentNode.id);
<a name="line698"></a>  assertEquals(1, rv.remainder);
<a name="line699"></a>
<a name="line700"></a>  goog.dom.getNodeAtOffset(node, 6, rv);
<a name="line701"></a>  assertEquals(&#39;67&#39;, rv.node.nodeValue);
<a name="line702"></a>  assertEquals(&#39;c&#39;, rv.node.parentNode.id);
<a name="line703"></a>  assertEquals(0, rv.remainder);
<a name="line704"></a>
<a name="line705"></a>  goog.dom.getNodeAtOffset(node, 23, rv);
<a name="line706"></a>  assertEquals(&#39;123&#39;, rv.node.nodeValue);
<a name="line707"></a>  assertEquals(&#39;g&#39;, rv.node.parentNode.id);
<a name="line708"></a>  assertEquals(2, rv.remainder);
<a name="line709"></a>
<a name="line710"></a>  goog.dom.getNodeAtOffset(node, 30, rv);
<a name="line711"></a>  assertEquals(&#39;7890&#39;, rv.node.nodeValue);
<a name="line712"></a>  assertEquals(&#39;i&#39;, rv.node.parentNode.id);
<a name="line713"></a>  assertEquals(3, rv.remainder);
<a name="line714"></a>
<a name="line715"></a>}
<a name="line716"></a>
<a name="line717"></a>// IE inserts line breaks and capitalizes nodenames.
<a name="line718"></a>function assertEqualsCaseAndLeadingWhitespaceInsensitive(value1, value2) {
<a name="line719"></a>  value1 = value1.replace(/^\s+|\s+$/g, &#39;&#39;).toLowerCase();
<a name="line720"></a>  value2 = value2.replace(/^\s+|\s+$/g, &#39;&#39;).toLowerCase();
<a name="line721"></a>  assertEquals(value1, value2);
<a name="line722"></a>}
<a name="line723"></a>
<a name="line724"></a>function testGetOuterHtml() {
<a name="line725"></a>  var contents = &#39;&lt;b&gt;foo&lt;/b&gt;&#39;;
<a name="line726"></a>  var node = document.createElement(&#39;div&#39;);
<a name="line727"></a>  node.setAttribute(&#39;foo&#39;, &#39;bar&#39;);
<a name="line728"></a>  node.innerHTML = contents;
<a name="line729"></a>  assertEqualsCaseAndLeadingWhitespaceInsensitive(
<a name="line730"></a>      goog.dom.getOuterHtml(node), &#39;&lt;div foo=&quot;bar&quot;&gt;&#39; + contents + &#39;&lt;/div&gt;&#39;);
<a name="line731"></a>
<a name="line732"></a>  var imgNode = document.createElement(&#39;img&#39;);
<a name="line733"></a>  imgNode.setAttribute(&#39;foo&#39;, &#39;bar&#39;);
<a name="line734"></a>  assertEqualsCaseAndLeadingWhitespaceInsensitive(
<a name="line735"></a>      goog.dom.getOuterHtml(imgNode), &#39;&lt;img foo=&quot;bar&quot;&gt;&#39;);
<a name="line736"></a>}
<a name="line737"></a>
<a name="line738"></a>
<a name="line739"></a>function testGetWindowFrame() {
<a name="line740"></a>  var frameWindow = window.frames[&#39;frame&#39;];
<a name="line741"></a>  var frameDocument = frameWindow.document;
<a name="line742"></a>  var frameDomHelper = new goog.dom.DomHelper(frameDocument);
<a name="line743"></a>
<a name="line744"></a>  // Cannot use assertEquals since IE fails on ===
<a name="line745"></a>  assertTrue(frameWindow == frameDomHelper.getWindow());
<a name="line746"></a>}
<a name="line747"></a>
<a name="line748"></a>function testGetWindow() {
<a name="line749"></a>  var domHelper = new goog.dom.DomHelper();
<a name="line750"></a>  // Cannot use assertEquals since IE fails on ===
<a name="line751"></a>  assertTrue(window == domHelper.getWindow());
<a name="line752"></a>}
<a name="line753"></a>
<a name="line754"></a>function testGetWindowStatic() {
<a name="line755"></a>  // Cannot use assertEquals since IE fails on ===
<a name="line756"></a>  assertTrue(window == goog.dom.getWindow());
<a name="line757"></a>}
<a name="line758"></a>
<a name="line759"></a>function testIsNodeList() {
<a name="line760"></a>  var elem = document.getElementById(&#39;p2&#39;);
<a name="line761"></a>  var text = document.getElementById(&#39;b2&#39;).firstChild;
<a name="line762"></a>
<a name="line763"></a>  assertTrue(&#39;NodeList should be a node list&#39;,
<a name="line764"></a>      goog.dom.isNodeList(elem.childNodes));
<a name="line765"></a>  assertFalse(&#39;TextNode should not be a node list&#39;,
<a name="line766"></a>      goog.dom.isNodeList(text));
<a name="line767"></a>  assertFalse(&#39;Array of nodes should not be a node list&#39;,
<a name="line768"></a>      goog.dom.isNodeList([elem.firstChild, elem.lastChild]));
<a name="line769"></a>}
<a name="line770"></a>
<a name="line771"></a>function testGetFrameContentDocument() {
<a name="line772"></a>  var iframe = document.getElementsByTagName(&#39;iframe&#39;)[0];
<a name="line773"></a>  var name = iframe.name;
<a name="line774"></a>  var iframeDoc = goog.dom.getFrameContentDocument(iframe);
<a name="line775"></a>  assertEquals(window.frames[name].document, iframeDoc);
<a name="line776"></a>}
<a name="line777"></a>
<a name="line778"></a>function testGetFrameContentWindow() {
<a name="line779"></a>  var iframe = document.getElementsByTagName(&#39;iframe&#39;)[0];
<a name="line780"></a>  var name = iframe.name;
<a name="line781"></a>  var iframeWin = goog.dom.getFrameContentWindow(iframe);
<a name="line782"></a>  assertEquals(window.frames[name], iframeWin);
<a name="line783"></a>}
<a name="line784"></a>
<a name="line785"></a>function testCanHaveChildren() {
<a name="line786"></a>  for (var tag in goog.dom.TagName) {
<a name="line787"></a>    var expected = true;
<a name="line788"></a>    switch (tag) {
<a name="line789"></a>      case goog.dom.TagName.APPLET:
<a name="line790"></a>      case goog.dom.TagName.AREA:
<a name="line791"></a>      case goog.dom.TagName.BR:
<a name="line792"></a>      case goog.dom.TagName.COL:
<a name="line793"></a>      case goog.dom.TagName.FRAME:
<a name="line794"></a>      case goog.dom.TagName.HR:
<a name="line795"></a>      case goog.dom.TagName.IMG:
<a name="line796"></a>      case goog.dom.TagName.INPUT:
<a name="line797"></a>      case goog.dom.TagName.IFRAME:
<a name="line798"></a>      case goog.dom.TagName.ISINDEX:
<a name="line799"></a>      case goog.dom.TagName.LINK:
<a name="line800"></a>      case goog.dom.TagName.NOFRAMES:
<a name="line801"></a>      case goog.dom.TagName.NOSCRIPT:
<a name="line802"></a>      case goog.dom.TagName.META:
<a name="line803"></a>      case goog.dom.TagName.OBJECT:
<a name="line804"></a>      case goog.dom.TagName.PARAM:
<a name="line805"></a>      case goog.dom.TagName.SCRIPT:
<a name="line806"></a>      case goog.dom.TagName.STYLE:
<a name="line807"></a>        expected = false;
<a name="line808"></a>    }
<a name="line809"></a>    var node = goog.dom.createDom(tag);
<a name="line810"></a>    assertEquals(tag + &#39; should &#39; + (expected ? &#39;&#39; : &#39;not &#39;) +
<a name="line811"></a>        &#39;have children&#39;, expected, goog.dom.canHaveChildren(node));
<a name="line812"></a>  }
<a name="line813"></a>}
<a name="line814"></a>
<a name="line815"></a>function testGetAncestorNoMatch() {
<a name="line816"></a>  var elem = goog.dom.$(&#39;nestedElement&#39;);
<a name="line817"></a>  assertNull(goog.dom.getAncestor(elem, function() {return false;}));
<a name="line818"></a>}
<a name="line819"></a>
<a name="line820"></a>function testGetAncestorMatchSelf() {
<a name="line821"></a>  var elem = goog.dom.$(&#39;nestedElement&#39;);
<a name="line822"></a>  var matched = goog.dom.getAncestor(elem, function() {return true;}, true);
<a name="line823"></a>  assertEquals(elem, matched);
<a name="line824"></a>}
<a name="line825"></a>
<a name="line826"></a>function testGetAncestorNoMatchSelf() {
<a name="line827"></a>  var elem = goog.dom.$(&#39;nestedElement&#39;);
<a name="line828"></a>  var matched = goog.dom.getAncestor(elem, function() {return true;});
<a name="line829"></a>  assertEquals(elem.parentNode, matched);
<a name="line830"></a>}
<a name="line831"></a>
<a name="line832"></a>function testGetAncestorWithMaxSearchStepsMatchSelf()  {
<a name="line833"></a>  var elem = goog.dom.$(&#39;nestedElement&#39;);
<a name="line834"></a>  var matched = goog.dom.getAncestor(
<a name="line835"></a>      elem, function() {return true;}, true, 2);
<a name="line836"></a>  assertEquals(elem, matched);
<a name="line837"></a>}
<a name="line838"></a>
<a name="line839"></a>function testGetAncestorWithMaxSearchStepsMatch() {
<a name="line840"></a>  var elem = goog.dom.$(&#39;nestedElement&#39;);
<a name="line841"></a>  var searchEl = elem.parentNode.parentNode;
<a name="line842"></a>  var matched = goog.dom.getAncestor(
<a name="line843"></a>      elem, function(el) {return el == searchEl;}, false, 1);
<a name="line844"></a>  assertEquals(searchEl, matched);
<a name="line845"></a>}
<a name="line846"></a>
<a name="line847"></a>function testGetAncestorWithMaxSearchStepsNoMatch() {
<a name="line848"></a>  var elem = goog.dom.$(&#39;nestedElement&#39;);
<a name="line849"></a>  var searchEl = elem.parentNode.parentNode;
<a name="line850"></a>  var matched = goog.dom.getAncestor(
<a name="line851"></a>      elem, function(el) {return el == searchEl;}, false, 0);
<a name="line852"></a>  assertNull(matched);
<a name="line853"></a>}
<a name="line854"></a>
<a name="line855"></a>function testGetAncestorByTagNameNoMatch() {
<a name="line856"></a>  var elem = goog.dom.$(&#39;nestedElement&#39;);
<a name="line857"></a>  assertNull(
<a name="line858"></a>      goog.dom.getAncestorByTagNameAndClass(elem, goog.dom.TagName.IMG));
<a name="line859"></a>}
<a name="line860"></a>
<a name="line861"></a>function testGetAncestorByTagNameOnly() {
<a name="line862"></a>  var elem = goog.dom.$(&#39;nestedElement&#39;);
<a name="line863"></a>  var expected = goog.dom.$(&#39;testAncestorDiv&#39;);
<a name="line864"></a>  assertEquals(expected,
<a name="line865"></a>      goog.dom.getAncestorByTagNameAndClass(elem, goog.dom.TagName.DIV));
<a name="line866"></a>}
<a name="line867"></a>
<a name="line868"></a>function testGetAncestorByClassNameNoMatch() {
<a name="line869"></a>  var elem = goog.dom.$(&#39;nestedElement&#39;);
<a name="line870"></a>  assertNull(
<a name="line871"></a>      goog.dom.getAncestorByTagNameAndClass(elem, null, &#39;bogusClassName&#39;));
<a name="line872"></a>}
<a name="line873"></a>
<a name="line874"></a>function testGetAncestorByClassName() {
<a name="line875"></a>  var elem = goog.dom.$(&#39;nestedElement&#39;);
<a name="line876"></a>  var expected = goog.dom.$(&#39;testAncestorP&#39;);
<a name="line877"></a>  assertEquals(expected,
<a name="line878"></a>      goog.dom.getAncestorByTagNameAndClass(elem, null, &#39;testAncestor&#39;));
<a name="line879"></a>}
<a name="line880"></a>
<a name="line881"></a>function testGetAncestorByTagNameAndClass() {
<a name="line882"></a>  var elem = goog.dom.$(&#39;nestedElement&#39;);
<a name="line883"></a>  var expected = goog.dom.$(&#39;testAncestorDiv&#39;);
<a name="line884"></a>  assertEquals(expected,
<a name="line885"></a>      goog.dom.getAncestorByTagNameAndClass(elem, goog.dom.TagName.DIV,
<a name="line886"></a>          &#39;testAncestor&#39;));
<a name="line887"></a>}
</pre>


</body>
</html>
